package com.ljc.controller;
import com.google.common.base.Splitter;
import com.ljc.model.Match;
import com.ljc.model.MatchHistory;
import com.ljc.model.User;
import com.ljc.service.IUserService;
import com.ljc.utils.MapParameter;
import  com.ljc.utils.dotahelper;
import com.ljc.model.Admin;
import com.ljc.service.AdminService;
import com.ljc.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;
import java.util.Map;

@Controller
public class adminIndexController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private IUserService iUserService;
    @Value("classpath:init.json")
    private Resource resource;


    @GetMapping("/adminindex")
    private String adminindex(HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");
        session.setAttribute("username",admin.getUserName());
        return "adminindex";
    }

    @GetMapping("/create")
    public String create(){

        return "admin/add";
    }

    @GetMapping("/createuser")
    public String create_u(){

        return "admin/adduser";
    }

    @GetMapping("/detail")
    public String det(HttpSession session){
        String a = (String) session.getAttribute("searchName");
        System.out.println(a);
        return "admin/detail";
    }
    @PostMapping("/getsearchdetail")
    @ResponseBody
    public Object gd(@RequestBody Admin admin, HttpSession session){
        System.out.println(admin.toString());
        session.setAttribute("searchName",admin.getUserName());
        return true;
    }

    @PostMapping("deleteadmin")
    @ResponseBody
    public Map<String,Object> deleteadmin(String ids){
       List <String> a  =Splitter.on(",").splitToList(ids);
       for(String id:a){
           Integer Id =  Integer.valueOf(id);
           adminService.delete(Id);

       }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("deleteuser")
    @ResponseBody
    public Map<String,Object> deleteuser(String ids){
        List <String> a  =Splitter.on(",").splitToList(ids);
        for(String id:a){

            iUserService.delete(id);

        }
        return MapControl.getInstance().success().getMap();
    }

    @GetMapping("/list")
    private String list(){

        return "admin/list";
    }

    @GetMapping("/userlist")
    private String userlist(){

        return "admin/userlist";
    }

    @GetMapping("/update")
    private String update(){

        return "admin/update";
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Admin admin,HttpSession session){
        try {
            System.out.println(admin.toString());
            int result = adminService.update(admin);
            if(result<=0){
                System.out.println("faild");
                return MapControl.getInstance().error().getMap();
            }
            System.out.println("success");
            return MapControl.getInstance().success().getMap();
        } catch (Exception e) {
            e.printStackTrace();
            return MapControl.getInstance().error().getMap();
        }
    }

    @PostMapping("/create")
    @ResponseBody
    public Map<String,Object> create_ad(@RequestBody Admin admin,HttpSession session){
        try {
            System.out.println(admin.toString());
            int result = adminService.create(admin);
            if(result<=0){
                System.out.println("faild");
                return MapControl.getInstance().error().getMap();
            }
            System.out.println("success");
            return MapControl.getInstance().success().getMap();
        } catch (Exception e) {
            e.printStackTrace();
            return MapControl.getInstance().error().getMap();
        }
    }


    @PostMapping("/createuser")
    @ResponseBody
    public Map<String,Object> create_user(@RequestBody User user,HttpSession session){
        System.out.println(user.toString());
        int result = iUserService.create(user);
        if(result<=0){
            System.out.println("faild");
            return MapControl.getInstance().error().getMap();
        }
        System.out.println("success");
        return MapControl.getInstance().success().getMap();
    }


    @PostMapping("query")
    @ResponseBody
    public Map query(Admin admin, ModelMap modelMap){

        List<Admin> list=adminService.query(admin);
        Integer count = adminService.count(admin);
        return MapControl.getInstance().page(list,count).getMap();
    }

    @PostMapping("queryuser")
    @ResponseBody
    public Map queryuser(User user, ModelMap modelMap){
        List<User> list=iUserService.query(user);
        Integer count = iUserService.count(user);
        return MapControl.getInstance().page(list,count).getMap();
    }

    @PostMapping("queryHero")
    @ResponseBody
    public Map queryHero(String steamid) throws IOException {
        System.out.println(steamid);
        List<Match> list=dotahelper.getmatch();
        return MapControl.getInstance().page(list,100).getMap();
    }

    @PostMapping("query1")
    @ResponseBody
    public Map query1(Admin admin, ModelMap modelMap){
        List<Admin> list=adminService.query(admin);
        Integer count = adminService.count(admin);
        return MapControl.getInstance().page(list,count).getMap();
    }
    @GetMapping("/details")
    public String details(Integer id,ModelMap modelMap){
        Admin admin = adminService.detail(id);
        modelMap.addAttribute("admin",admin);
        return "admin/update";
    }

    @GetMapping("/menu")
    @ResponseBody
    public void menu(HttpServletResponse response) {
        try {
            File file = resource.getFile();
            InputStream inputStream = new FileInputStream(file);
            //转换流
            InputStreamReader isr = new InputStreamReader(inputStream,"utf-8");
            BufferedReader bufferedReader = new BufferedReader(isr);
            String str;
            StringBuffer sb = new StringBuffer();
            while ((str = bufferedReader.readLine()) !=null){
                sb.append(str);
            }
            bufferedReader.close();
            isr.close();
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
